/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
#ifndef _HTRS_MIMEDATA_RESOURCE_HEADER_
#define _HTRS_MIMEDATA_RESOURCE_HEADER_

/********************************************************************\
                            Includes
\********************************************************************/
#include <QString>
#include "htr/htrglobal.h"
#include "htr/mansock.h"
#include "htrsresource.h"

/********************************************************************\
                          Declaration
\********************************************************************/
class HtrsMimeDataResource : public HtrsResource
{
  Q_OBJECT
  Q_PROPERTY(QByteArray mimeData READ mimeData WRITE setMimeData STORED true)
  Q_PROPERTY(QString    mimeType READ mimeType WRITE setMimeType STORED true)

  public:
    Q_INVOKABLE HtrsMimeDataResource(const QString &url = "");
    virtual ~HtrsMimeDataResource();

    bool       open(qint32 openMode);
   
    void       setMimeData(const QByteArray &data);
    QByteArray mimeData() const;

    void       setMimeType(const QString &mimeType);
    QString    mimeType() const;
    
    void       setPsUrl(const QString &url);
    QString    psUrl() const;

    // never delete me..
    void       setPermanent(bool isPermanent);

  signals:
    void stateChanged(HtrsMimeDataResource    *res, int newState);
    void mimeTypeChanged(HtrsMimeDataResource *res, const QString &newMimeType);
    void mimeDataChanged(HtrsMimeDataResource *res, const QByteArray &newMimeData);

  public slots:
    void       close(QObject *session);
 
  protected:
    virtual void setState(Htr::Res::State state);
    virtual void handleMsg(HtrManagedSocket *sock);
    virtual void handleOpenMsg(HtrManagedSocket *sock);
    virtual void handleCloseMsg(HtrManagedSocket *sock);
    virtual void handleMimeDataMsg(HtrManagedSocket *sock);
    virtual void handleMimeTypeMsg(HtrManagedSocket *sock);

  private:
    void     commitMimeType();
    void     commitMimeData();
    void     commitState();
    void     store();

    QString            mMimeType;
    QByteArray         mMimeData;
    Htr::Res::State    mState;

    Htr::Res::OpenModeFlag mWriteLevel;
    bool               mIsPersistent;
    bool               mIsPermanent;
    QList<QObject*>    mFeeders;    // "Writers"
    QList<QObject*>    mFollowers;  // "Readers"
};

#endif 
